package cc.mrbird.febs.buss.util;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.DocumentHelper;
import org.dom4j.io.SAXReader;
/**
 * xml解析 解析海岩短信平台返回的数据
 * @author dell
 *
 */
public class PaserXmlStr {
    public static void main(String[] args) throws Exception {
        String xmlStr = "<?xml><returnsms><returnstatus>status</returnstatus><message>message</message><remainpoint> remainpoint</remainpoint><taskID>taskID</taskID><successCounts>successCounts</successCounts></returnsms>";
        StringBuffer sb=new StringBuffer("<returnsms><callbox><mobile>15538326939</mobile><taskid>39956600</taskid><content><![CDATA[ 测试1 ]]></content><receivetime>2019-09-24 13:03:48</receivetime><extno><![CDATA[ 091 ]]></extno></callbox><callbox><mobile>15538326939</mobile><taskid>39956600</taskid><content><![CDATA[ 测试2 ]]></content><receivetime>2019-09-24 13:04:32</receivetime><extno><![CDATA[ 091 ]]></extno></callbox></returnsms>");
        xmlStr=sb.toString();
        long begin = System.currentTimeMillis();
        /*parse4Dom(xmlStr);
        long after = System.currentTimeMillis();
        System.out.println("DOM用时" + (after - begin) + "毫秒");
        begin = System.currentTimeMillis();
        parse4Sax(xmlStr);
        after = System.currentTimeMillis();
        System.out.println("SAX用时" + (after - begin) + "毫秒");*/
 
        begin = System.currentTimeMillis();
        parse4DOM4j(xmlStr);
        long after = System.currentTimeMillis();
        System.out.println("DOM4j用时" + (after - begin) + "毫秒");
        System.out.println("开始解析----");
        //PaserXmlStr.stringToXmlByDom4j(sb.toString());
    }

    /**
     * 解析xml返回上行短信内容
     * @param content
     * @return
     */
	public static List<Map> stringToMapByDom4j(String content) {
		//上行短信内容
		//StringBuffer sb=new StringBuffer("<returnsms><callbox><mobile>15538326939</mobile><taskid>39956600</taskid><content><![CDATA[ 测试1 ]]></content><receivetime>2019-09-24 13:03:48</receivetime><extno><![CDATA[ 091 ]]></extno></callbox><callbox><mobile>15538326939</mobile><taskid>39956600</taskid><content><![CDATA[ 测试2 ]]></content><receivetime>2019-09-24 13:04:32</receivetime><extno><![CDATA[ 091 ]]></extno></callbox></returnsms>");
        
		List<Map> mapList=new ArrayList<Map>();
		HashMap<String, Object> result = new HashMap<String, Object>();
		try {
			SAXReader saxReader = new SAXReader();
			org.dom4j.Document docDom4j = saxReader.read(new ByteArrayInputStream(content.getBytes("utf-8")));
			org.dom4j.Element root = docDom4j.getRootElement();
			List<Attribute> rooAttrList = root.attributes();
			for (Attribute rootAttr : rooAttrList) {
				System.out.println(rootAttr.getName() + ": " + rootAttr.getValue());
				result.put(rootAttr.getName(), rootAttr.getValue());
			}
			List<org.dom4j.Element> childElements = root.elements();
			for (org.dom4j.Element e1 : childElements) {
				System.out.println("第一层：" + e1.getName() + ": " + e1.getText());
				result.put(e1.getName(), e1.getText());
			}

			for (org.dom4j.Element child : childElements) {
				// 未知属性名情况下
				/*List<Attribute> attributeList = child.attributes();
				for (Attribute attr : attributeList) {
					
					System.out.println("第二层aa：" + attr.getName() + ": " + attr.getValue());
					result.put(attr.getName(), attr.getValue());
				}*/
				// 已知属性名情况下
				// System.out.println("id: " + child.attributeValue("id"));

				// 未知子元素名情况下
				Map map=new HashMap<String,String>();
				List<org.dom4j.Element> elementList = child.elements();
				for (org.dom4j.Element ele : elementList) {
					map.put(ele.getName(), ele.getText());
					System.out.println("第二层bb：" + ele.getName() + ": " + ele.getText());
					result.put(ele.getName(), ele.getText());
				}
				mapList.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mapList;
	}
    
    
	/**
     * @description DOM4J解析,需要下载对应的jar包
     * @param xmlStr
     * @throws Exception
     */
    public static Map parse4DOM4j(String xmlStr) throws Exception {
    	System.out.println(xmlStr);
        org.dom4j.Document doc=(org.dom4j.Document)DocumentHelper.parseText(xmlStr);   
        org.dom4j.Element message = doc.getRootElement();
        System.out.println("根节点" + message.getName());
        Iterator elements = message.elementIterator();  
        Map map=new HashMap<String,String>();
        while(elements.hasNext()){
           org.dom4j.Element element = (org.dom4j.Element)elements.next();   
           System.out.println("节点" + element.getName() + "\ttext=" + element.getText());
           System.out.println();
           map.put(element.getName(), element.getText());
        }
        return map;
    }
 
 
}
